package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.title.TitleBarView;
import com.bumptech.glide.Glide;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.BrandListInfo;
import com.chexiehui.jc.mvp.car.BrandListCovenant;
import com.chexiehui.jc.mvp.car.BrandListPresenter;
import com.chexiehui.jc.ui.activity.SpecialCarSeriesGroupActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.widget.LoadingLayout;
import com.chexiehui.jc.widget.indexbar.IndexBar;
import com.chexiehui.jc.widget.indexbar.suspension.SuspensionDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: SongJun.
 * CreateDate: 2019/1/21 0021.
 * Description:选择品牌
 */

public class BrandListFragment extends BaseMvpFragment<BrandListPresenter> implements BrandListCovenant.View {
    @BindView(R.id.titleBar)
    TitleBarView titleBar;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.ib_right_index_bar)
    IndexBar ibRightIndexBar;
    @BindView(R.id.tv_side_bar_hint)
    TextView tvSideBarHint;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;

    private CommonAdapter<BrandInfo> carBrandAdapter;
    private List<BrandInfo> brandListData;
    private SuspensionDecoration suspensionDecoration;

    public static BrandListFragment getInstance() {
        BrandListFragment fragment = new BrandListFragment();
        return fragment;
    }

    @Override
    protected void initTitleView(Bundle savedInstanceState) {
        titleBar.setTitleMainText("新车批发")
                .setLeftTextDrawable(0);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_brand_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        recycler.setLayoutManager(layoutManager);
        brandListData = new ArrayList<>();
        carBrandAdapter = new CommonAdapter<BrandInfo>(mActivity, R.layout.item_car_garage_new_brand, brandListData) {
            @Override
            protected void convert(com.zhy.adapter.recyclerview.base.ViewHolder holder, BrandInfo brandInfo, int position) {
                holder.setText(R.id.tv_item_name, brandInfo.getName());
                Glide.with(mActivity)
                        .load(brandInfo.getImg())
                        .into((ImageView) holder.getView(R.id.iv_item_icon));
            }
        };
        carBrandAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                startActivity(SpecialCarSeriesGroupActivity.class, new BundleBuilder().putSerializable("entity", brandListData.get(position))
                        .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(carBrandAdapter);
        suspensionDecoration = new SuspensionDecoration(mActivity, brandListData);
        recycler.addItemDecoration(suspensionDecoration);
        //indexbar初始化
        ibRightIndexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(false)//设置需要真实的索引
                .setSourceDatasAlreadySorted(true)
                .setmLayoutManager(layoutManager);//设置RecyclerView的LayoutManager
        mvpPresenter.getList();
    }

    @Override
    protected BrandListPresenter createPresenter() {
        return new BrandListPresenter(this);
    }

    @Override
    public void onGetListSuccess(BaseModel<List<BrandListInfo>> successResult) {
        List<BrandInfo> list = new ArrayList<>();
        for (int i = 0; i < successResult.getData().size(); i++) {
            for (int j = 0; j < successResult.getData().get(i).getBrands().size(); j++) {
                successResult.getData().get(i).getBrands().get(j).setSpell(successResult.getData().get(i).getSpell());
            }
            list.addAll(successResult.getData().get(i).getBrands());
        }
        brandListData.clear();
        brandListData.addAll(list);
        carBrandAdapter.notifyDataSetChanged();
        ibRightIndexBar.setmSourceDatas(brandListData)//设置数据
                .invalidate();
        suspensionDecoration.setmDatas(brandListData);
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText(failureResult.getResultInfo());
        } else {
            loadingLayout.setStatus(LoadingLayout.Error);
            loadingLayout.setErrorText(failureResult.getResultInfo());
        }
    }

}
